package com.link.play.fit.ai.ui.view

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.graphics.Shader
import android.graphics.SweepGradient
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import com.link.play.fit.ai.utils.ScreenUtil

class LoadLinearView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    // 修改参数：水平渐变相关
    private val rectRadius = ScreenUtil.dpToPx(16f)
    private val path = Path()
    private val matrix = Matrix()
    private var gradientOffset = 0f


//    private val rectRadius = ScreenUtil.dpToPx(16f) // 圆角半径
//    private val path = Path() // 圆角路径

    private val solidPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.FILL
    }

    private val gradientPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
        strokeWidth = ScreenUtil.dpToPx(12f)
    }

    // 绘制参数（保持不变）
//    private val solidPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
//        style = Paint.Style.FILL
//        color = Color.BLUE
//    }
//
//    private val gradientPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
//        style = Paint.Style.STROKE
//        strokeWidth = ScreenUtil.dpToPx(12f)
//        strokeCap = Paint.Cap.ROUND
//    }
    // 颜色配置
    private val startColor = Color.parseColor("#FFD9D9D9")
    private val endColor = Color.parseColor("#FFF8F7F7")

    // 新增渐变相关
    private lateinit var linearGradient: LinearGradient
    private var animator: ValueAnimator? = null

    // 颜色配置保持不变
    var firstColor: Int = Color.parseColor("#216CCD")
//    var startColor: Int = Color.parseColor("#FF2E73CC")
    var startColor2: Int = Color.parseColor("#FF3A7BCE")
    var centerColor: Int = Color.parseColor("#FF699DDE")
    var centerColor2: Int = Color.parseColor("#FF79A3D9")
    var centerColor3: Int = Color.parseColor("#FF91B5E4")
    var centerColor4: Int = Color.parseColor("#FF7CA9E4")
    var endColor1: Int = Color.parseColor("#FF3A7BCE")
    var endColor12: Int = Color.parseColor("#FF2E73CC")

    private val gradientColors = intArrayOf(
        firstColor,startColor,startColor2,centerColor,centerColor2,centerColor4,endColor1,endColor12
    )
    private var sweepGradient: SweepGradient? = null

    // 动画控制
    private var rotateAnimator: ValueAnimator? = null
    private var rotateDegree = 0f
        set(value) {
            field = value
            invalidate()
        }

    // 状态控制
    private var isArcMode = false
    private var arcStartAngle = 0f

    init {
        startRotation()
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)

        // 初始化水平渐变
        linearGradient = LinearGradient(
            0f, 0f,
            width * 2f, 0f, // 双倍宽度保证循环效果
            intArrayOf(startColor, endColor, startColor,startColor,startColor,startColor,startColor,startColor),
//            floatArrayOf(0.2f, 0.5f, 0.8f),
            floatArrayOf(0.2f, 0.5f, 0.8f, 0.8f, 0.6f, 0.7f,0.6f, 0.7f),
            Shader.TileMode.MIRROR
        )

        solidPaint.shader = linearGradient

        // 圆角路径
        path.reset()
        path.addRoundRect(
            RectF(0f, 0f, w.toFloat(), h.toFloat()),
            rectRadius,
            rectRadius,
            Path.Direction.CW
        )

        startAnimation()
    }

    override fun onDraw(canvas: Canvas) {
        canvas.clipPath(path)

        // 更新渐变位置
        matrix.setTranslate(gradientOffset, 0f)
        linearGradient.setLocalMatrix(matrix)

        // 绘制实心背景
        canvas.drawRoundRect(
            RectF(0f, 0f, width.toFloat(), height.toFloat()),
            rectRadius,
            rectRadius,
            solidPaint
        )
    }
     fun startAnimation() {
        animator?.cancel()

        animator = ValueAnimator.ofFloat(0f, width * 2f).apply {
            duration = 1000
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener {
                gradientOffset = it.animatedValue as Float
                invalidate()
            }
            start()
        }
    }

    private fun startRotation() {
        rotateAnimator?.cancel()
        rotateAnimator = ObjectAnimator.ofFloat(0f, 360f).apply {
            duration = 2000
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener {
                rotateDegree = it.animatedValue as Float
            }
            start()
        }
    }

    fun startArcRotation(){
        isArcMode = true
        invalidate()
    }

    fun stopRotation(){
//        rotateAnimator?.cancel()
        animator?.cancel()
    }
    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        animator?.cancel()
    }
//    override fun onDetachedFromWindow() {
//        super.onDetachedFromWindow()
//        rotateAnimator?.cancel()
//    }
}